package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.domain.SystemDictionaryItem;
import cn.wolfcode.crud.qo.QueryObject;
import cn.wolfcode.crud.qo.SystemDictionaryItemQuery;
import cn.wolfcode.crud.service.ISystemDictionaryItemService;
import cn.wolfcode.crud.service.ISystemDictionaryService;
import cn.wolfcode.crud.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;
    //表示有其中一个就可以访问该页面
    @RequiresPermissions(value = {"systemDictionaryItem:list","字典明细目录"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")SystemDictionaryItemQuery qo) {
        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        //查询所有的字典目录,共享给字典明细页面
        model.addAttribute("systemDictionarys",systemDictionaryService.listAll());
        return  "systemDictionaryItem/list";
    }


    @RequestMapping("/delete")
    @RequiresPermissions(value = {"systemDictionaryItem:delete","字典明细删除"},logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){

            if (id != null) {
                systemDictionaryItemService.delete(id);
            }
            return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"systemDictionaryItem:saveOrUpdate","字典明细新增/编辑"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){

            if (systemDictionaryItem.getId() != null) {
                systemDictionaryItemService.update(systemDictionaryItem);
            }else {
                systemDictionaryItemService.save(systemDictionaryItem);
            }
            return new JsonResult();

    }
}
